package com.wg.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

/**
 * 字符操作
 *
 * @author QuCeng
 */
public class StringUtil  {

    private static final Logger logger = LoggerFactory.getLogger(StringUtil.class);

    private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static final String DEFAULT_BOUND_SYMBOL = ","; // 平台默认连接符（1,2,3）

    public static final String DATA_STATISTICS_HASH_KEY_COMBINER = "&&"; // 统计数据REIDS KEY连接符

    /**
     * 字符串转换成double 数
     *
     * @param strDouble
     * @return
     */
    public static double toDouble(String strDouble) {
        if (strDouble == null || strDouble.trim().length() == 0) {
            return 0d;
        }

        try {
            return Double.parseDouble(strDouble);
        } catch (Exception e) {
            return 0d;
        }
    }

    /**
     * 字符串转换成int 数
     *
     * @param strInt
     * @return
     */
    public static int toInt(String strInt) {
        if (strInt == null || strInt.trim().length() == 0) {
            return 0;
        }

        try {
            return Integer.parseInt(strInt);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 把指定的数据转化为16进制格式的字符串
     *
     * @param data
     *            待转化的数据
     * @return 16进制表示的数据
     */
    public static String toHexString(byte[] data) {

        return toHexString(data, 0, data.length);
    }

    /**
     * 把指定的数据转化为16进制格式的字符串， 如toHexString({8,9,12,4},1,3) = "090C"
     *
     * @param data
     *            待转化的数据
     * @param beginIndex
     *            需转化的数据的起始索引
     * @param endIndex
     *            需转化的数据的结束索引
     * @return 16进制表示的数据
     */
    public static String toHexString(byte[] data, int beginIndex, int endIndex) {

        if (data == null || beginIndex < 0)
            return null;
        StringBuilder strBuilder = new StringBuilder();
        for (int i = beginIndex; i < endIndex; i++) {
            strBuilder.append(hexDigits[data[i] >>> 4 & 0xf]);
            strBuilder.append(hexDigits[data[i] & 0xf]);
        }
        return strBuilder.toString();
    }



    public static String trim(String str) {

        return str == null ? "" : str.trim();
    }

    public static String generateLoginToken() {
        String s = UUID.randomUUID().toString();

        return s.replaceAll("-", "");
    }



}
